import _ from 'lodash'
import Qs from 'qs';
import { sprintf } from 'sprintf-js';

export interface Params {
    [key: string]: any;
}

export interface CommonRsp<T> {
    result: number
    // message: string
    // data: T
}

// const getHost = (path: string) => {
//     return 'http://assets.zvod.badambiz.com/';
// };

export async function vGet(url: string, params: Params = {}): Promise<CommonRsp<any>> {
    try {
        url = `${url}`;// ?${Qs.stringify(params, {indices: false})}
        console.log("url: "+url)
        const r = await fetch(url, {});
        if (!r.ok) {
            throw 'Network fail';
        }
        const rspText = await r.text();
        console.log('rspText: ' + rspText);
        // const rsp = JSON.parse(rspText);


        // if (rsp.result !== 0) {
        //     console.warn(
        //         sprintf(
        //             'req %s err result %d message %s',
        //             host + path + '?' + Qs.stringify(params),
        //             rsp.result,
        //             rsp.message,
        //         ),
        //     );
        // }
        return rspText;
    } catch (e) {
        console.warn(`path  : GET ${url}\nparams: ${JSON.stringify(params, null, 2)}\nerror : ${e}`);
        return {result: -99999, message: e, data: {}};
    }
}
